package org.ahead4.joint.task;

import org.ahead4.joint.common.JointUtils;
import org.ahead4.joint.dto.MachineRecord;
import org.ahead4.joint.dto.PumpRecord;
import org.ahead4.joint.dto.RequestEnvelope;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author WangYu
 * @title: JointScheduleTask
 * @description: 定时执行发送任务
 * @date 2020/3/1320:21
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SendScheduleTask {
    private static final Log logger = LogFactory.getLog(SendScheduleTask.class);
    @Value("${joint.send.base.url}")
    String baseUrl;
    @Value("${joint.send.machine.record.url}")
    private String sendMachineRecordUrl;
    @Value("${joint.send.pump.record.url}")
    private String sendPumpRecordUrl;
    @Value("${joint.send.appid}")
    private String appid;
    @Value("${joint.send.secret-key}")
    private String secretKey;
    @Value("${joint.send.secret-key-type}")
    private String secretKeyType;
    // 站码
    @Value("${joint.send.stcd}")
    private String stcd;

    /**
     * 发送泵机监测记录
     */
    // 添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    // 或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    public void sendMachineRecord() {

        // 单条数据
        MachineRecord machineRecord = new MachineRecord();
        machineRecord.setStcd(stcd);
        machineRecord.setCode(0);
        machineRecord.setStatus("01");
        machineRecord.setTm(new Date());
        machineRecord.setConsumption(BigDecimal.ONE);
        machineRecord.setDischarge(BigDecimal.ONE);
        machineRecord.setElectricity(BigDecimal.ONE);
        machineRecord.setHour(BigDecimal.ONE);
        machineRecord.setVoltage(BigDecimal.ONE);
        List<MachineRecord> list = new ArrayList<>();
        list.add(machineRecord);
        // 构建签名信封对象
        RequestEnvelope requestEnvelope = RequestEnvelope.buildMD5(appid, list, secretKey);
        JointUtils.send(baseUrl + sendMachineRecordUrl, requestEnvelope);
        logger.info(new StringBuilder("成功发送泵机监测记录").append(list.size()).append("条").append("\n")
                .append("RequestEnvelope:").append(requestEnvelope.toJSONString()));
    }

    /**
     * 发送泵站监测记录
     */
    // 添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    // 或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    public void sendPumpRecord() {
        // 单条数据
        PumpRecord pumpRecord = new PumpRecord();
        pumpRecord.setStcd(stcd);
        pumpRecord.setTm(new Date());
        pumpRecord.setBeforeWater(BigDecimal.ZERO);
        // 无该指标传null
        pumpRecord.setOutsideWater(null);
        List<PumpRecord> list = new ArrayList<>();
        list.add(pumpRecord);
        // 构建签名信封对象
        RequestEnvelope requestEnvelope = RequestEnvelope.buildMD5(appid, list, secretKey);
        JointUtils.send(baseUrl + sendPumpRecordUrl, requestEnvelope);
        logger.info(new StringBuilder("发送泵站监测记录").append(list.size()).append("条").append("\n")
                .append("RequestEnvelope:").append(requestEnvelope.toJSONString()));
    }
}
